<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Schedule extends CI_Controller {

     public function __construct(){
        parent::__construct();
        $this->load->model('LoginModel');        

        if(!$this->LoginModel->checkLogin()){
            $this->json(['code'=>-1, 'data'=>'not login']);
        }
    }


    public function lists()
    {
        //$this->load->view('welcome_message');
        $type= $this->input->get('type');
        
        $this->load->model('ScheduleModel');
        if($type =='reserve'){
            $list = $this->ScheduleModel->getScheduleList();
        }else{
            $list = $this->ScheduleModel->getList(time());
        }

        $rs = true;
        if($rs){
            $this->json([
                'code' => 0,
                'msg' => "success", 
                'data' => $list, 
            ]);
        }else{
            $this->json([
                'code' => -1,
                'data' => "fail" 
            ]);
        }
    }


    public  function add(){
        $userId = $this->LoginModel->getUserWxId(); 
        $seatId = $this->input->post('seatId');
        $shiftId = $this->input->post('shiftId');
        $operationTime = date("Y-m-d H:i:s", time());

        $this->load->model('ScheduleModel');

        //check if seatId and shiftId exist
        $check = $this->ScheduleModel->get($seatId, $shiftId);

        if($check){
            return $this->json(['code'=>-1, 'msg'=>'seat not avail']);
        }

        $result = $this->ScheduleModel->add($userId, $seatId, $shiftId, $operationTime);
        if(!$result){
            return $this->json(['code'=>-1, 'msg'=>'fail']);
        }else{
            return $this->json(['code'=>0, 'msg'=>'success']);
        }
    }

    public function cancel(){

        $userId = $this->LoginModel->getUserWxId(); 
        $seatId = $this->input->post('seatId');
        $shiftId = $this->input->post('shiftId');

        $this->load->model('ScheduleModel');
        $check = $this->ScheduleModel->checkCancel($userId, $seatId, $shiftId);
        if(!$check){
            return $this->json(['code'=>-1, 'msg'=>'seat could not cancel']);
        }
        
        $cancelTime = date("Y-m-d H:i:s", time());

        $result = $this->ScheduleModel->cancel($userId, $seatId, $shiftId, $cancelTime);
        if(!$result){
            return $this->json(['code'=>-1, 'msg'=>'fail']);
        }else{
            return $this->json(['code'=>0, 'msg'=>'success']);
        }
    }

    //for driver to confirm the schedule
    public function confirm(){

    }

    public function getStatus(){
        $userId = $this->LoginModel->getUserWxId(); 
        $shiftIds = $this->input->get('shiftIds');

        $this->load->model('ScheduleModel');
        $result = $this->ScheduleModel->getStatus($shiftIds);

        if($result){
            $this->json(['code' => 0, 'msg' => "success", 'data' => $result]);
        }else{
            $this->json(['code' => -1,'data' => "fail"]);
        }
    }

    public function getArrivingStatus(){
        $userId = $this->LoginModel->getUserWxId(); 
        $shiftIds = $this->input->get('shiftIds');

        $this->load->model('ScheduleModel');
        $result = $this->ScheduleModel->getArrivingStatus($shiftIds);

        if($result){
            $this->json(['code' => 0, 'msg' => "success", 'data' => $result]);
        }else{
            $this->json(['code' => -1,'data' => "fail"]);
        }
    
    }
}

